# -*- coding: utf-8 -*-
import json
import re
import scrapy
from scrapy_redis.spiders import RedisSpider


class QqmSpider(RedisSpider):
    name = 'qqm'
    allowed_domains = ['qq.com']
    # start_urls = ['https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg?platform=yqq&categoryId=10000000&sortId=5&sin=0&ein=59&outCharset=utf-8']
    redis_key = "qqmusic"
    detail_url = "https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&disstid={}&outCharset=utf-8&platform=yqq"

    def parse(self, response):
        res_str = response.body.decode()
        dissid_list=re.findall(r'{"dissid":"(\d*?)",',res_str)
        for dissid in dissid_list:
            gedan_url=self.detail_url.format(dissid)
            yield scrapy.Request(gedan_url,callback=self.parse_gedan_detail)
    def parse_gedan_detail(self,response):
        response_str=response.body.decode()
        gedan_str=response_str.replace("jsonCallback(", "").replace(")", "")
        gedan_dict=json.loads(gedan_str)
        yield gedan_dict
